package com.group.service;

import com.group.dao.GeneralBeanDao;
import com.group.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MessageService {
	@Autowired
	GeneralBeanDao beanDao;

	/**
	 * 添加一条评论消息
	 * 
	 * @param reply
	 */
	public void addMessage(TopicReply reply) {
		Topic topic = beanDao.quickQueryOne(Topic.class, "id", reply.getTopicId());
		if (topic.getUserId() == null)
			return;

		// 我的帖子,我回复不发送消息
		if (topic.getUserId().longValue() == reply.getUserId()) {
			return;
		}

		Message message = new Message();
		message.setContent(reply.getContent());
		message.setCreateTime(null);
		message.setTopicReplyId(reply.getId());
		message.setSenderId(reply.getUserId());
		message.setType(1);
		message.setUserId(topic.getUserId());
		message.setTopicId(reply.getTopicId());
		beanDao.save(message);
	}

	/**
	 * 添加一条关注消息
	 * 
	 * @param userId
	 * @param followUserId
	 */
	public void addFans(Long userId, Long followUserId) {
		Message message = new Message();
		message.setSenderId(userId);
		message.setUserId(followUserId);
		message.setType(0);

		// 判断是否曾经关注过，如果有忽略这条消息
		if (beanDao.queryOne(message) != null) {
			return;
		}

		beanDao.save(message);
	}

	/**
	 * 添加是否关注
	 * 
	 * @param messages
	 */
	public void isLike(List<Message> messages, Long userId) {
		for (Message message : messages) {
			UserFollow para = new UserFollow();
			para.setUserId(userId);
			para.setFollowUserId(message.getSenderId());
			if (beanDao.queryOne(para) != null) {
				message.setIsLike(true);
			} else {
				message.setIsLike(false);
			}

		}
	}

}
